﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace eBirdAttend
{
    public partial class EnrollToeOffice : UserControl
    {
        public string _strPort = "";
        public string _strCardID;
        eOfficeTrans _eOfficeTrans;

        public EnrollToeOffice()
        {
            InitializeComponent();
        }

        private void eOfficeSettings_Load(object sender, EventArgs e)
        {
            cmbPort.SelectedIndex = 0;
            try
            {
                // MessageBox.Show(Properties.Settings.Default["PORT"].ToString());
                cmbPort.ResetText();
                cmbPort.SelectedItem = Properties.Settings.Default["PORT"].ToString();

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }
        }

        private void btnEnroll_Click(object sender, EventArgs e)
        {
            if (cmbPort.SelectedIndex != 0)
            {
                timer1.Start();
                labelMesg.Visible = true;
                _strPort = cmbPort.SelectedItem.ToString();
                Properties.Settings.Default["PORT"] = cmbPort.SelectedItem.ToString(); 
                Properties.Settings.Default.Save();

                _eOfficeTrans = new eOfficeTrans();
                _eOfficeTrans.Init();
                _eOfficeTrans.SetTextBox(richTextBox1);
                _eOfficeTrans.SetLogFile("CardEnrollLog.txt");

                if (!_eOfficeTrans.OpenPort(_strPort))
                {
                    MessageBox.Show("Failed to open port,Close e-OfficeDownLoad Application and try again", "Port Open failed");
                    return;
                }
                _eOfficeTrans.SetTimeOut(30);//time out set as 5 sec
                string strRep = "";
                string DevCmd = "EN " + _strCardID + "\r\n";
                //MessageBox.Show("Swipe card after closing this messagebox", "Enroll Card");
                if (_eOfficeTrans.SendCommand(DevCmd, ref strRep) > 0)
                {
                    MessageBox.Show("Enrolled");
                }
                else
                {
                    MessageBox.Show("Unable to enroll in device");
                }
                _eOfficeTrans.Close();
                timer1.Stop();
                labelMesg.Visible = false;
               
            }
            else
            {
                MessageBox.Show("Select the port");
                cmbPort.Focus();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (_eOfficeTrans != null) _eOfficeTrans.Close();            
             this.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (labelMesg.Visible) labelMesg.Visible = false;
            else labelMesg.Visible = true;
        }
    }
}
